
#include <assert.h>
#include <sharedvaluemanager.h>


ceSharedValueManager* ceSharedValueManager::static_instance = 0;

ceSharedValueManager::ceSharedValueManager ()
{
}

ceSharedValueManager* ceSharedValueManager::Get ()
{
	if (!ceSharedValueManager::static_instance)
		{
			ceSharedValueManager::static_instance = new ceSharedValueManager ();
		}
	
	return ceSharedValueManager::static_instance;
}


void ceSharedValueManager::AddValue(iSharedValue *value)
{
  assert (value);
	if (value->GetName() != "")
		{
			_indices[value->GetName()] = _values.size();
		}
	_values.push_back(value);
  value->AddRef();
}

int ceSharedValueManager::GetIdx(const std::string &name) const
{
	std::map<std::string, int>::const_iterator it = _indices.find(name);
	if (it == _indices.end())
		{
			return -1;
		}
	return it->second;
}

iSharedValue* ceSharedValueManager::GetValue(int idx)
{
	if (idx < 0 || idx >= _values.size ())
		{
			return 0;
		}
	return _values[idx];
}

iSharedValue* ceSharedValueManager::GetValue(const std::string &name)
{
	return GetValue(GetIdx(name));
}
